package com.example.huangzejie.web;

import com.example.huangzejie.dao.UserMapper;
import com.example.huangzejie.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@RequestMapping("user")
@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;
    @RequestMapping("indexhtml")
    public String list(ModelMap map){
        map.put("userList", userMapper.list());
        return "/index";
    }
    @RequestMapping("add")
    public String add(){
        return "/add";
    }

    @PostMapping("add")
    public String add(User user){
        userMapper.add(user);
        return "redirect:/user/indexhtml";
    }
    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        userMapper.delete(id);
        return "redirect:/user/indexhtml";
    }
    @RequestMapping("update/{id}")
    public String update(@PathVariable("id") Integer id,User user,ModelMap map){
        map.put("user",user);
        return "/update";
    }
    @PostMapping("update")
    public String edit(User user){
        userMapper.update(user);
        return "redirect:/user/indexhtml";
    }
}
